package org.jeecg.modules.gui.service.impl;

import org.jeecg.modules.gui.entity.GuiTestOrderMain;
import org.jeecg.modules.gui.entity.GuiTestOrderCustom;
import org.jeecg.modules.gui.entity.GuiTestOrderTicket;
import org.jeecg.modules.gui.mapper.GuiTestOrderCustomMapper;
import org.jeecg.modules.gui.mapper.GuiTestOrderTicketMapper;
import org.jeecg.modules.gui.mapper.GuiTestOrderMainMapper;
import org.jeecg.modules.gui.service.IGuiTestOrderMainService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: GUI订单管理
 * @Author: jeecg-boot
 * @Date:   2020-06-29
 * @Version: V1.0
 */
@Service
public class GuiTestOrderMainServiceImpl extends ServiceImpl<GuiTestOrderMainMapper, GuiTestOrderMain> implements IGuiTestOrderMainService {

	@Autowired
	private GuiTestOrderMainMapper guiTestOrderMainMapper;
	@Autowired
	private GuiTestOrderCustomMapper guiTestOrderCustomMapper;
	@Autowired
	private GuiTestOrderTicketMapper guiTestOrderTicketMapper;
	
	@Override
	@Transactional
	public void saveMain(GuiTestOrderMain guiTestOrderMain, List<GuiTestOrderCustom> guiTestOrderCustomList,List<GuiTestOrderTicket> guiTestOrderTicketList) {
		guiTestOrderMainMapper.insert(guiTestOrderMain);
		for(GuiTestOrderCustom entity:guiTestOrderCustomList) {
			//外键设置
			entity.setOrderId(guiTestOrderMain.getId());
			guiTestOrderCustomMapper.insert(entity);
		}
		for(GuiTestOrderTicket entity:guiTestOrderTicketList) {
			//外键设置
			entity.setOrderId(guiTestOrderMain.getId());
			guiTestOrderTicketMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(GuiTestOrderMain guiTestOrderMain,List<GuiTestOrderCustom> guiTestOrderCustomList,List<GuiTestOrderTicket> guiTestOrderTicketList) {
		guiTestOrderMainMapper.updateById(guiTestOrderMain);
		
		//1.先删除子表数据
		guiTestOrderCustomMapper.deleteByMainId(guiTestOrderMain.getId());
		guiTestOrderTicketMapper.deleteByMainId(guiTestOrderMain.getId());
		
		//2.子表数据重新插入
		for(GuiTestOrderCustom entity:guiTestOrderCustomList) {
			//外键设置
			entity.setOrderId(guiTestOrderMain.getId());
			guiTestOrderCustomMapper.insert(entity);
		}
		for(GuiTestOrderTicket entity:guiTestOrderTicketList) {
			//外键设置
			entity.setOrderId(guiTestOrderMain.getId());
			guiTestOrderTicketMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		guiTestOrderCustomMapper.deleteByMainId(id);
		guiTestOrderTicketMapper.deleteByMainId(id);
		guiTestOrderMainMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			guiTestOrderCustomMapper.deleteByMainId(id.toString());
			guiTestOrderTicketMapper.deleteByMainId(id.toString());
			guiTestOrderMainMapper.deleteById(id);
		}
	}
	
}
